<?php

namespace Fluency\Utils;

/**
 * A basic singly linked list that allows head-tail traversal.
 *
 * @package Fluency\Utils
 * @author  Ignas Rudaitis
 * @since   1.0
 */
class HeadTailList
{
    private $head;
    private $tail;

    /**
     * The constructor. It constructs a list from a non-empty array of node (element) values.
     *
     * @throws InvalidArgumentException if the node value array is empty
     *
     * @param mixed[] $nodes
     */
    public function __construct(array $nodes)
    {
        if (empty($nodes)) {
            throw new \InvalidArgumentException("The provided array contains no nodes");
        }
        $this->head = array_shift($nodes);
        if (!empty($nodes)) {
            $this->tail = new self($nodes);
        }
    }

    /**
     * Gets the head (the first node) of the list.
     *
     * @return mixed
     */
    public function getHead()
    {
        return $this->head;
    }

    /**
     * Returns the tail (the list without the first node) of the list.
     *
     * @return Fluency\Utils\HeadTailList
     */
    public function getTail()
    {
        return $this->tail;
    }

    /**
     * Checks if the list has more than one node.
     *
     * @return boolean
     */
    public function hasTail()
    {
        return $this->tail !== null;
    }
}
